<template lang="jade">
	el-dialog(:visible.sync="visible",:title="title",v-loading="loading")
		el-form(:model="loginModel",ref="form",v-if="!cuurentUserInfo")
			el-form-item(prop="usernameOrEmailAddress",:rules="[{required:true,message:'请输入用户名或密码'}]")
				el-input(placeholder="账号或者email",v-model="loginModel.usernameOrEmailAddress")
			el-form-item(prop="password",:rules="[{required:true,message:'请输入密码'}]")
				el-input(placeholder="登录密码",type="password",v-model="loginModel.password")
			el-form-item
				div
					el-button(type="primary",@click="login") 登录
		div(v-else,class="alert alert-danger", role="alert")
			strong 无操作权限！
			span 您当前无权限操作该功能

</template>
<script>
    import  _ from 'lodash';
	import {request} from 'collection';
	import axios from 'axios';

    export default {
	    data(){
	        return {
				loading: false,
				loginModel:{
					rememberMe: true,
					usernameOrEmailAddress:null,
					password: null
				},
				cuurentUserInfo: null
			}
		},
		props:{visible: {type:Boolean,default:false}},
		computed:{
			title(){ return this.cuurentUserInfo ? '您无权限进行该操作': '您还没登陆，请先登录'}
		},
	    methods:{
			get_current_user_info(){
				var vm = this;
				request.get.call(this,'/api/services/app/account/getCurrentLoginInformations',this.loginModel).then((res)=>{
					vm.cuurentUserInfo=res.data.result.user;
				});
			},
			login(){
				var vm = this;
				this.$refs.form.validate((valid)=>{
					if(!valid) return;
					request.post.call(vm,'/api/services/app/account/login',this.loginModel).then((res)=>{
						if(res.response && !res.response.data.success) return;
						vm.visible=false;
						location.reload();
					}).catch(()=>{
						vm.visible=true;
					});
				});
			}
		},
        mounted(){
			this.get_current_user_info();
		}
    }
</script>
<style lang="scss">
	
</style>